package com.taylor.community.controller;

import com.taylor.community.service.DataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;

/**
 * @author 26925
 */
@Controller
public class DataController {
    @Autowired
    private DataService dataService;

    /**
     * 统计页面
     * @return
     */
    @RequestMapping(path = "/data",method = {RequestMethod.GET,RequestMethod.POST})
    public String getData(){
        //管理员才有权限
        return "/site/admin/data";
    }
    @RequestMapping(path = "/data/uv",method = RequestMethod.POST)
    /**
     * @DateTimeFormat可以将前端传过来的日期格式化
     */
    public String getUv(@DateTimeFormat(pattern = "yyyy-MM-dd") Date start,
                        @DateTimeFormat(pattern = "yyyy-MM-dd") Date end, Model model){
        long uv = dataService.calculateUv(start,end);
        model.addAttribute("uvResult",uv);
        model.addAttribute("uvStartDate",start);
        model.addAttribute("uvEndDate",end);
        //表示这个请求没有处理完 交给另一个请求继续处理，其实就是走上面的方法转发给模板"/site/admin/data"
        return "forward:/data";
    }
    @RequestMapping(path = "/data/dau",method = RequestMethod.POST)
    /**
     * @DateTimeFormat可以将前端传过来的日期格式化
     */
    public String getDau(@DateTimeFormat(pattern = "yyyy-MM-dd") Date start,
                        @DateTimeFormat(pattern = "yyyy-MM-dd") Date end, Model model){
        long dau = dataService.calculateDau(start,end);
        model.addAttribute("dauResult",dau);
        model.addAttribute("dauStartDate",start);
        model.addAttribute("dauEndDate",end);
        //表示这个请求没有处理完 交给另一个请求继续处理
        return "forward:/data";
    }
}
